package GUIv2;

import general.FileUtils;
import general.GlobalProperties;

import java.io.File;

import javax.swing.JOptionPane;

import jsonObjects.Files;
import jsonObjects.Profile;

import org.apache.log4j.Logger;

import serverHandling.HttpRequestHandlers;
import serverHandling.SessionObjects;

public class FileVersioningGUI extends javax.swing.JPanel {
    private static final long serialVersionUID = -7642494283598805599L;
    private static Logger log = Logger.getLogger(FileVersioningGUI.class);

    public FileVersioningGUI(File file, Profile profile) {
        this.profile = profile;
        this.file = file;
        initComponents();
    }
    @SuppressWarnings("serial")
    private void initComponents() {
        Object[][] futureMenuItems;

        Files tmp = SessionObjects.getFileFromProfile(profile.getName(),
                file.getName());
        if (tmp != null) {
            profileFileList = HttpRequestHandlers.versioning(tmp.getId());

            if (profileFileList.length != 0) {
                futureMenuItems = new Object[profileFileList.length][profileFileList.length];
                for (int i = 1; i < profileFileList.length; i++) {
                    futureMenuItems[i][0] = profileFileList[i].getName();
                    futureMenuItems[i][1] = profileFileList[i].getCreated();
                }
            } else
                futureMenuItems = new Object[0][0];
        } else
            futureMenuItems = new Object[0][0];

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        cancelButton = new javax.swing.JButton();
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        nextButton = new javax.swing.JButton();
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Name", "Version"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                    false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        cancelButton.setText("Return");

        nextButton.setText("Next");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 5, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(cancelButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(nextButton)))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(nextButton)
                                        .addComponent(cancelButton))
                                .addContainerGap())
        );
    }

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {

        if (jTable1.getSelectedRowCount() == 1) {
            try {
                int row = jTable1.getSelectedRow();
                int column = jTable1.getSelectedColumn();
                String urObjctInCell = (String) jTable1.getValueAt(row, column - 1);
                Files fileToDownload = null;
                for (Files a : profileFileList)
                    if (a.getName().equals(urObjctInCell)) {
                        fileToDownload = a;
                        continue;
                    }

                if (fileToDownload != null) {
                    File toDelete = new File(GlobalProperties.getFolderMonit()
                            + profile.getName() + "\\" + file.getName());
//                    FileUtils.deleteFileUnsafe(toDelete);
                    HttpRequestHandlers.download(fileToDownload,GlobalProperties.getFolderMonit() + profile.getName() + "\\");
                }

                JOptionPane.showMessageDialog(null, "Success");
            } catch (Exception e) {
                log.error(e.getMessage());
            }

            VersioningReplacePanel replacePanel = new VersioningReplacePanel();
            javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();
            parenPanel.removeAll();
            javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
            parenPanel.setLayout(parenPanelLayot);
            parenPanelLayot.setHorizontalGroup(
                    parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
            );
            parenPanelLayot.setVerticalGroup(
                    parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
            );
            parenPanel.add(replacePanel);
            parenPanel.revalidate();
            parenPanel.repaint();

        } else {
            JOptionPane.showMessageDialog(null, "Nalezy wybrac plik!");
        }
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        VersioningReplacePanel replacePanel = new VersioningReplacePanel();

        javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();
        parenPanel.removeAll();

        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();
    }

    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton nextButton;

    private File file;
    private Profile profile;

    private Files[] profileFileList;
}
